#define _CRT_SECURE_NO_WARNINGS
#include"Slist.h"
Slistnode* newslist(Slistepy x)
{
	Slistnode* newnode = (Slistnode*)malloc(sizeof(Slistnode));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void slistprint(Slistnode* pphead)
{
	Slistnode* cur = pphead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL");
}
void slistpushback(Slistnode** pphead, Slistepy x)
{
	Slistnode* newnode = newslist(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		Slistnode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void slistpushfront(Slistnode** pphead, Slistepy x)
{
	Slistnode* newnode = newslist(x);
	newnode->next = *pphead;
	*pphead = newnode;

}
void slistpopback(Slistnode** pphead)
{
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		*pphead == NULL;
	}
	else
	{
		Slistnode* tail = *pphead;
		Slistnode* pre = NULL;
		while (tail->next != NULL)
		{
			pre = tail;
			tail = tail->next;
		}
		free(tail);
		pre->next = NULL;
	}
}
void slistpopfront(Slistnode** pphead)
{
	Slistnode* cur = (*pphead)->next;
	free(*pphead);
	*pphead = cur;
}
Slistnode* slistfind(Slistnode* phead, Slistepy x)
{
	Slistnode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void slistinsert(Slistnode** pphead, Slistnode** pos, Slistepy x)
{
	Slistnode* newnode = newslist(x);
	Slistnode* cur = *pphead;
	while (cur->next != *pos)
	{
		cur=cur->next;
	}
	cur->next = newnode;
	newnode->next = *pos;
}
void slisterase(Slistnode** pphead, Slistnode** pos)
{
	Slistnode* cur = *pphead;
	while (cur->next != *pos)
	{
		cur = cur->next;
	}
	cur->next = (*pos)->next;
	free(*pos);
}